package com.com.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class Testcollection  {
    public  static  void main(String []args){

        //使用arraylist直接在iterator中remove会报错，应该使用CopyOnWriteArrayList
        Collection persons=new ArrayList();
        ((ArrayList) persons).add(new Person("xcy","11"));
        ((ArrayList) persons).add(new Person("lj","22"));
        ((ArrayList) persons).add(new Person("xxx","33"));
        Iterator iterator =persons.iterator();
/*        for (int i=0;i<persons.size();i++)
        {
            Person person= (Person) ((CopyOnWriteArrayList) persons).get(i);
            //System.out.println(person);
            if (person.getName().equals("xcy"))
            {
                persons.remove(person);
            }
            else
            {
                System.out.println(person);

            }
        }*/

        while(iterator.hasNext()) {
            Person person= (Person) iterator.next();
            if (person.getName().equals("xxx"))
            {
                persons.remove(person);
            }
            else
            {
                System.out.println(person);

            }
        }

    }
}
